package com.lianniu.mgr.baseService.organization.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.organization.model.OrganizationMagVo;
import com.lianniu.mgr.baseService.organization.service.IOrganizationMagService;


@Controller
@RequestMapping("/rest")
public class OrgRestController {
  private static Logger logger = Logger.getLogger(OrgRestController.class);
  @Autowired
  private IOrganizationMagService organizationservice;
  
  /**
   * 
   * @Description：TODO(获取组织列表) 
   * @Param:@param req
   * @Param:@param vo
   * @Param:@return    设定文件 
   * @Return:BaseResult    返回类型 
   * @throws:

   * @Date:2016年3月29日
   */
  @RequestMapping(value="/organization/getorgls",method={RequestMethod.GET,RequestMethod.POST})
  public @ResponseBody BaseResult toGetOrgLs(HttpServletRequest req, OrganizationMagVo vo){
    logger.info("OrgRestController toGetOrgLs begin.");
    try{
      logger.info("IOrganizationMagService getOrganizationLs begin.");
      BaseResult br = organizationservice.getOrganizationLs(vo);
      logger.info("IOrganizationMagService getOrganizationLs end.");
      logger.info("OrgRestController toGetOrgLs end.");
      return br;
    }catch(Exception e){
      logger.error(e.getMessage());
      return BaseResult.exception(e.getMessage());
    }
  }
  /**
   * 
   * @Description：TODO(获取组织编号) 
   * @Param:@param req
   * @Param:@return    设定文件 
   * @Return:BaseResult    返回类型 
   * @throws:

   * @Date:2016年3月29日
   */
  @RequestMapping(value="/organization/getno",method={RequestMethod.GET,RequestMethod.POST})
  public @ResponseBody BaseResult toGetOrgNo(HttpServletRequest req){
    logger.info("OrgRestController toGetOrgLs begin.");
    try {
      logger.info("IOrganizationMagService getOrgNo begin.");
      BaseResult br = organizationservice.getOrgNo();
      logger.info("IOrganizationMagService getOrgNo end.");
      logger.info("OrgRestController toGetOrgLs end.");
      return br;
    } catch (Exception e) {
      // TODO Auto-generated catch block
      logger.error(e.getMessage());
      return BaseResult.exception(e.getMessage());
    }
  }
  /**
   * 
   * @Description：TODO(添加组织) 
   * @Param:@param req
   * @Param:@param vo
   * @Param:@return    设定文件 
   * @Return:BaseResult    返回类型 
   * @throws:

   * @Date:2016年3月29日
   */
  @RequestMapping(value="/organization/addorg",method={RequestMethod.GET,RequestMethod.POST})
  public @ResponseBody BaseResult toAdd(HttpServletRequest req,OrganizationMagVo vo){
    logger.info("OrgRestController toAdd begin.");
    try {
      logger.info("IOrganizationMagService addNewOrganization begin.");
      BaseResult br = organizationservice.addNewOrganization(vo);
      logger.info("IOrganizationMagService addNewOrganization end.");
      logger.info("OrgRestController toAdd end.");
      return br;
    } catch (Exception e) {
      // TODO Auto-generated catch block
      logger.error(e.getMessage());
      return BaseResult.exception(e.getMessage());
    }
  }
  /**
   * 
   * @Description：TODO(编辑组织) 
   * @Param:@param req
   * @Param:@param vo
   * @Param:@return    设定文件 
   * @Return:BaseResult    返回类型 
   * @throws:

   * @Date:2016年3月29日
   */
  @RequestMapping(value="/organization/editorg",method={RequestMethod.GET,RequestMethod.POST})
  public @ResponseBody BaseResult toEdit(HttpServletRequest req,OrganizationMagVo vo){
    logger.info("OrgRestController toEdit begin.");
    try {
      logger.info("IOrganizationMagService updateOrganization begin.");
      BaseResult br = organizationservice.editOrganization(vo);
      logger.info("IOrganizationMagService updateOrganization end.");
      logger.info("OrgRestController toEdit end.");
      return br;
    } catch (Exception e) {
      // TODO Auto-generated catch block
      logger.error(e.getMessage());
      return BaseResult.exception(e.getMessage());
    }
  }
  /**
   * 
   * @Description：TODO(删除组织) 
   * @Param:@param req
   * @Param:@param idLs
   * @Param:@return    设定文件 
   * @Return:BaseResult    返回类型 
   * @throws:

   * @Date:2016年3月29日
   */
  @RequestMapping(value="/organization/delorg",method={RequestMethod.GET,RequestMethod.POST})
  public @ResponseBody BaseResult toDelete(HttpServletRequest req,String idLs,String modifyUser){
    logger.info("OrgRestController toDelete begin.");
    String[] ids = idLs.split(";");
    try {
      logger.info("IOrganizationMagService deleteOrganization begin.");
      BaseResult br = organizationservice.delOrganization(ids,modifyUser);
      logger.info("IOrganizationMagService deleteOrganization end.");
      logger.info("OrgRestController toDelete end.");
      return br;
    } catch (Exception e) {
      // TODO Auto-generated catch block
      logger.error(e.getMessage());
      return BaseResult.exception(e.getMessage());
    }
  }
  /**
   * 
   * @Description：TODO(获取上级组织列表) 
   * @Param:@param req
   * @Param:@param maxlevel
   * @Param:@return    设定文件 
   * @Return:BaseResult    返回类型 
   * @throws:

   * @Date:2016年4月5日
   */
  @RequestMapping(value="/organization/getparent",method={RequestMethod.GET,RequestMethod.POST})
  public @ResponseBody BaseResult toGetParentLs(HttpServletRequest req,String maxlevel){
    try {
      logger.info("OrgRestController toGetParentLs begin.");
      BaseResult br = organizationservice.getOrgNameByLevel(maxlevel);
      logger.info("OrgRestController toGetParentLs end.");
      return br;
    } catch (Exception e) {
      // TODO Auto-generated catch block
      logger.error(e.getMessage());
      return BaseResult.exception(e.getMessage());
    }
  }
/*  @RequestMapping(value="/organization/chaoscheck",method={RequestMethod.GET,RequestMethod.POST})
  public @ResponseBody BaseResult toCheckName(String param,ModelMap map){
    if(param.equals("yes")){
      map.put("status", "y");
      map.put("info", "该组织名可用");
    }else{
      map.put("status", "n");
      map.put("info", "该组织名已存在");
    }
    return BaseResult.success(JSONObject.toJSONString(map));
  }*/
}
